<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Review extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('review_model');

		$this->template->set_breadcrumb('Nhận xét về sản phẩm', 'backend/review');
	}

	public function index()
	{
		//$this->auth->check_uri_permissions(true, 'backend/login');
		$this->template->title('SiteName', 'Nhận xét về sản phẩm');
		$this->template->build('backend/review/review', $this->data);
	}

	public function load($order_by = 'position', $order_dir = 'asc', $page = 1)
	{
		$pager_config['base_url'] = base_path() . "backend/review/load/{$order_by}/{$order_dir}/";
		$pager_config['uri_segment'] = 6;
		$pager_config['num_links'] = 3;
		$pager_config['total_rows'] = $this->review_model->count(array('status' => 1));
		$pager_config['per_page'] = 20;

		$limit = $pager_config['per_page'];
		$offset = $page ? (($page - 1) * $limit) : 0;
		$reviews = $this->review_model->find(array('status' => 1), "{$order_by} {$order_dir}", $limit, $offset)->result();
		$data['sql'] = $this->db->last_query();

		$data['rows'] = "";
		foreach ($reviews as $row)
		{
			$data['rows'].= "<tr><td><input type=\"checkbox\" name=\"items[]\" value=\"{$row->id}\" /></td>";
			$data['rows'].= "	<td>" . $row->name . "</td>";
			$data['rows'].= "	<td>" . $row->description . "</td>";
			$data['rows'].= "	<td><input type=\"text\" class=\"input ui-corner-all position\" name=\"item_positions[{$row->id}]\" value=\"{$row->position}\" /></td>";
			$data['rows'].= "	<td align=\"center\"><div><a href=\"". base_path() . "backend/review/update/{$row->id}\" class=\"edit\">Edit</a></div></td></tr>";
		}

		$data['from_record'] 	= $pager_config['total_rows'] == 0 ? 0 : $offset + 1;
		$data['to_record'] 	= $offset + $limit > $pager_config['total_rows'] ? $pager_config['total_rows'] : $offset + $limit;
		$data['total_record'] = $pager_config['total_rows'];

		$this->pagination->initialize($pager_config);
		$data['pagination'] = $this->pagination->create_links();
		$data['message'] = 'OK';

		echo json_encode($data);
	}

	public function create()
	{
		$this->data['message'] = '';
		$validator = $this->form_validation;
		$validator->set_rules('name', 'Tên Nhận xét về sản phẩm', 'trim|required|xss_clean|min_length[3]|max_length[32]|callback_check_review|alpha_dash');
		$validator->set_rules('description', 'Mô tả', 'trim|xss_clean');
		$validator->set_rules('image', 'Hình ảnh', 'trim|xss_clean');
		if($validator->run())
		{
			$data['name'] = $validator->set_value('name');
			$data['image'] = $validator->set_value('image');
			$data['description'] = $validator->set_value('description');
			$data['created'] = date('Y-m-d H:i:s');

			if($this->review_model->insert($data))
			{
				$this->data['message'] = 'Dữ liệu thêm vào thành công';
			}
			else
			{
				$this->data['message'] = 'Có lỗi xảy ra';
			}
		}
		else
		{
			$this->data['message'] = validation_errors('<span>', '</span>');
		}

		$this->template->set_breadcrumb('Tạo mới', 'backend/review/create');
		$this->template->title('SeoShop', 'Nhận xét về sản phẩm', 'Tạo mới');
		$this->template->build('backend/review/create', $this->data);
	}

	public function delete($review_id = NULL)
	{
		if($this->input->is_ajax_request())
		{
			if($review_ids = $this->input->post('items'))
			{
				foreach ((array)$review_ids as $review_id)
				{
					$this->review_model->delete(array('id' => $review_id));
				}
			}

			$data['message'] = 'OK';
			die(json_encode($data));
		}
	}

	public function update($review_id = NULL)
	{
		if($review_id)
		{
			$this->data['message'] = '';

			$validator = $this->form_validation;
			$validator = $this->form_validation;
			$validator->set_rules('name', 'Tên Nhận xét về sản phẩm', 'trim|required|xss_clean|min_length[3]|max_length[32]|callback_check_review|alpha_dash');
			$validator->set_rules('description', 'Mô tả', 'trim|xss_clean');
			$validator->set_rules('image', 'Hình ảnh', 'trim|xss_clean');

			if($validator->run())
			{
				$data['name'] = $validator->set_value('name');
				$data['image'] = $validator->set_value('image');
				$data['description'] = $validator->set_value('description');

				$this->review_model->update(array('id' => $review_id), $data);
				$this->data['message'] = 'Update success';
			}
			else
			{
				$this->data['message'] = validation_errors('<span>', '</span>');
			}

			$this->data['review'] = $this->review_model->find(array('id' => $review_id))->row();
			$this->template->title('SiteName', 'Nhận xét về sản phẩm', 'Chỉnh sửa');
			$this->template->build('backend/review/update', $this->data);
		}
		else
		{
			show_404();
		}
	}

	public function update_position()
	{
		if($this->input->is_ajax_request())
		{
			if($item_positions = $this->input->post('item_positions'))
			{

				foreach ((array)$item_positions as $item => $position)
				{
					$this->review_model->update(array('id' => $item), array('position' => $position));
				}
			}

			$data['message'] = 'OK';
			die(json_encode($data));
		}
	}

	public function check_review($review)
	{

	}
}